package com.jcnet.bizaia;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.jcnet.bizaia.bean.BusinessCategoryBean;
import com.jcnet.bizaia.bean.CatgoryBean;
import com.jcnet.bizaia.bean.CityBean;
import com.jcnet.bizaia.bean.CountryBean;
import com.jcnet.bizaia.bean.DiscussSearchInfoBean;
import com.jcnet.bizaia.bean.SPSetting;
import com.jcnet.bizaia.bean.SearchBean;
import com.jcnet.bizaia.commCategory.BusinessCategoryList;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.xstream.XmlCacheHelper;
import com.loopj.android.http.RequestParams;

public class DiscussSearchActivity extends SherlockActivity implements OnItemClickListener , OnClickListener{

	private XmlCacheHelper cacheHelper ;

	private DataStorageHelper dataStorageHelper;

	public static final int RESULT_CODE="DiscussSearchActivity".hashCode();
	
	private int mark = -1;
	
	
	private BroadcastReceiver broadcastReceiver;
	
	public static final String DISS_BISNUESS_RECEIVER = "DISS_BISNUESS_RECEIVER";
	
	
	private EditText editText;
	
	private DiscussSearchInfoBean discussSearchInfoBean;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initActionBars();
		cacheHelper = new XmlCacheHelper(this);
		dataStorageHelper = new DataStorageHelper(this, SPSetting.SETTING_TABLE_NAME);
		setContentView(R.layout.discuss_search);
		
		editText = (EditText)findViewById(R.id.EditText_Search);
		editText.setText("  ");
		discussSearchInfoBean = new DiscussSearchInfoBean();
		
		broadcastReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if(intent.getAction()==DISS_BISNUESS_RECEIVER){
					List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
					List<CatgoryBean> catgoryBeans = (List<CatgoryBean>) intent.getExtras().getSerializable("catgoryBeans");
					if(catgoryBeans!=null){
						for (CatgoryBean catgoryBean : catgoryBeans) {
							searchBeans.add(new SearchBean(catgoryBean.getCname(), catgoryBean.getCid()));
						}
					}
					
					Intent intents =new Intent(context, SearchListActivity.class);
					intents.putExtra("listVa", (Serializable)searchBeans);
					startActivityForResult(intents, Intent.EXTRA_DOCK_STATE_CAR);
				}
				
				if(intent.getAction()==VodSearchActivity.RECEIVER_VODSEARCHACTIVITY){
					List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
					List<BusinessCategoryBean> catgoryBeans = (List<BusinessCategoryBean>) intent.getExtras().getSerializable("AllBusinessCategoryLisyBean");
					if(catgoryBeans!=null){
						for (BusinessCategoryBean catgoryBean : catgoryBeans) {
							searchBeans.add(new SearchBean(catgoryBean.getIndustryname(), catgoryBean.getIid()));
						}
					}
					
					Intent intents =new Intent(context, SearchListActivity.class);
					intents.putExtra("listVa", (Serializable)searchBeans);
					startActivityForResult(intents, Intent.EXTRA_DOCK_STATE_CAR);
					
					System.out.println("come on baby!!!!!!!!");
				}
				
			}
		};
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(VodSearchActivity.RECEIVER_VODSEARCHACTIVITY);
		intentFilter.addAction(DiscussSearchActivity.DISS_BISNUESS_RECEIVER);
		registerReceiver(broadcastReceiver, intentFilter);
		
	}


	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//getSupportMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}


		return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		unregisterReceiver(broadcastReceiver);
	}
	
	private void initActionBars() {
		// TODO Auto-generated method stub
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowTitleEnabled(true);  //是否显示标题 m
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bg));
		setTitle(getText(R.string.search));
		actionBar.setSubtitle(getText(R.string.disucessSearch));
		actionBar.show();
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		System.out.println("1111111111");
		if(resultCode==DiscussSearchActivity.RESULT_CODE){

			System.out.println("??????????????????");
			
			SearchBean searchBean = (SearchBean) data.getExtras().getSerializable("resultBean");
			switch (mark) {
			case 1:
				
				((TextView)findViewById(R.id.adressTitle_Diss)).setText(searchBean.getSearchName());
				
				discussSearchInfoBean.setSearchAdressID(searchBean.getSearchID());
				
				break;
			case 2:
				
				break;
			case 3:
				((TextView)findViewById(R.id.BusinessTitle_Diss)).setText(searchBean.getSearchName());
				
				discussSearchInfoBean.setSearchCategoryID(searchBean.getSearchID());
				break;

			default:
				break;
			}
			
		}
	}
	
	
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
		switch (v.getId()) {
		case R.id.adressLin:


			List<CityBean> cityBeans = new ArrayList<CityBean>();   //二级城市列表
			Map<String, CountryBean> allAdressData = new HashMap<String, CountryBean>();
			allAdressData = (Map<String, CountryBean>) cacheHelper.Xml2Object(FirstInAdressActivity.CACHE_DATA_ADRESS);


			//根据第一次进入选择的国家  得到这个国家所有的城市
			CountryBean countryBean =  allAdressData.get(dataStorageHelper.get(dataStorageHelper.get(SPSetting.CHOOSE_COUNTRY_NAME), "China"));  //国家级别

			if(countryBean!=null){
				cityBeans  =  countryBean.getDistrictsJson();
			}

			if(cityBeans!=null){
				for (CityBean cityBean : cityBeans) {
					searchBeans.add(new SearchBean(cityBean.getDisname(), cityBean.getDisid()));
				}
			}

			Intent intent =new Intent(DiscussSearchActivity.this, SearchListActivity.class);
			intent.putExtra("listVa", (Serializable)searchBeans);
			mark = 1;
			startActivityForResult(intent,Intent.EXTRA_DOCK_STATE_CAR);

			break;
			
			
		case R.id.dataLin:
			new DatePickerDialog(DiscussSearchActivity.this, new OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
					String dateString = year+"-"+monthOfYear+"-"+dayOfMonth;
					
					((TextView)findViewById(R.id.DataTitle_Diss)).setText(dateString);
					
					discussSearchInfoBean.setSearchDate(dateString);
					
				}
			}, new Date().getYear(), new Date().getMonth(), new Date().getDay()).show();
			 
			break;

	
		case R.id.businessLin:
			mark = 3;
			RequestParams params = new RequestParams();
			params.put("languageNum", App.localeCode);
			params.put("midNum", 1);  
			new  BusinessCategoryList(DiscussSearchActivity.this, params).getAllCategory();
			break;

			
			
		case R.id.discussSearchButton:   //点击提交
			
			discussSearchInfoBean.setSerarchTitltString(editText.getText().toString().trim());
			
			Intent intent2 = new Intent(DiscussSearchActivity.this, SearchResultActivity.class);
			intent2.putExtra("searchItem", discussSearchInfoBean);
			startActivity(intent2);
			break;
		default:
			break;
		}


	}

 

	

}
